//
//  UIImage+image.m
//  UI进阶-裁剪
//
//  Created by 王龙飞 on 15/8/2.
//  Copyright (c) 2015年 王龙飞. All rights reserved.
//

#import "UIImage+image.h"

@implementation UIImage (image)
+ (UIImage *)imageWithBorderW:(CGFloat)borderW color:(UIColor *)color image:(UIImage *)image
{
    
    CGFloat circleW = image.size.width + 2 * borderW;
    CGFloat circleH = image.size.height + 2 * borderW;
    CGRect bigCircle = CGRectMake(0, 0, circleW, circleH);
    
    //1.获取位图的上下文
    UIGraphicsBeginImageContextWithOptions(bigCircle.size, NO, 0);
    
    //2.拼接路径
    //2.1绘制大圆
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:bigCircle];
    
    [color set];
    
    [path fill];
    
    //2.2绘制图片裁剪路径
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
    
    //裁剪图片路径
    [clipPath addClip];
    
    //添加图片
    [image drawAtPoint:CGPointMake(borderW, borderW)];
    
    //从上下文中获取图片
    image = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭上下文
    UIGraphicsEndImageContext();
    
    //返回图片
    return image;
    
}

@end
